<template>
<div style="z-index:7;left: 0;top: 0;position: fixed;height: 100%;width: 100%;background-color:white;">
    <div class="zhezhao"></div>
    <img src="../../../../assets/changepsw/back.png" class="img_back" @click="goback">
    <div class="div1">
       <span>{{oldphone?'更换':'绑定'}}</span>号码
    </div>
    <div class="div2">
        <div style="font-size:30px"><span v-show="!oldphone">绑定</span><span v-show="oldphone">更换</span>手机号码</div>
        <div style="font-size: 12px;margin-top: 5px;color: rgb(138, 138, 138);">
            可通过新的手机号码找回密码、验证身份。</div>
    </div>
    <div class="div2" style="margin-top:122px;padding-top:0px;padding-bottom:0px">
        <span style="font-size: 16px;color: rgb(44, 146, 255);">+86</span>
        <input type="text" class="input" placeholder="请输入新的手机号" v-model="phone">
        <img src="../../../../assets/changepsw/false.png" class="img_false" v-show="phone" @click="deletephone">
    </div>
    <div class="div3" v-show="ErrorMessage" :style="ErrorPosition+ErrorPosition2">
        <img src="../../../../assets/changepsw/error.png" class="img_error">
        <span style="margin-left:15px">{{oldphone==phone?'该手机号与当前绑定的手机号相同':'手机号错误，请重新输入'}}</span>
    </div>
    <div class="div4" @click="next()" :style="CandSubmit?'background-color: rgb(255, 150, 215);':''">下一步</div>
    <ImgPhone class="img_phone" :style="ShowLater?'opacity:1':'opacity:0'"></ImgPhone>
</div>
</template>

<script>
import ImgPhone from './img/phone/img_phone.vue'
export default {
    name:'newphone',
    components:{ImgPhone},
    data(){
        return{
            oldphone:'',
            phone:'',
            username:'',
            ErrorMessage:0,
            ErrorPosition:'',
            ErrorPosition2:'',
            CandSubmit:0,
            ShowLater:0
        }
    },
    methods:{
        goback(){this.$router.replace({name:'checkphone'})},
        deletephone(){this.phone=''},
        next(){
            if(this.CandSubmit){
                if(this.phone == this.oldphone){
                    this.ErrorMessage=1
                }else{
                    this.$addr.get(`sms/${this.username}/${this.phone}/1341374/send`).then(
                        response=>{
                            console.log('发送验证码请求成功',response.data);
                            // console.log(response.data.data[0].code);
                            if(response.data.data[0].code == 'Ok'){
                                this.$router.replace({name:'checkCode',params:{'username':this.username,'phone':this.phone}})
                            }else{
                                this.ErrorMessage=1
                            }
                        },error=>{
                            this.$message({
                                message:'发送验证码请求失败，请重新登陆',
                                type:'error'
                            })
                            console.log("发送验证码请求失败",error.message);
                            //this.$router.replace({name:'login'})
                        }
                    )
                }
            }
        }
    },
    watch:{
        ErrorMessage(value){
            if(value == 1){
                // let num = -295
                let num = -40
                let ok = 0
                var id = setInterval(() => {
                    this.ErrorPosition='margin-top:'+num+'px'
                    // console.log(this.ErrorPosition,'@');
                    if(num < -60){
                        ok = 1
                        num++
                    }else if(ok){
                        if(num > -55){
                            clearInterval(id);
                        }else{
                            num++
                        }
                    }else{
                        num--
                    }
                }, 3);
                var id3 = setTimeout(() => {
                    let num2=1
                    var id2 = setInterval(() => {
                        //下面的分号非常细节
                        this.ErrorPosition2=';opacity:'+num2
                        // console.log(this.ErrorPosition2,'%');
                        if(num2 < 0.05){
                            clearInterval(id2)
                            clearTimeout(id3)
                            this.ErrorMessage=0
                            this.ErrorPosition=this.ErrorPosition2=''
                        }else{
                            num2-=0.05
                        }
                    }, 15);
                }, 1000);
            }
        },
        phone(value){
            this.CandSubmit=0
            if(value.length == 11 && (Number(value)+1).toString()!='NaN'){
                this.CandSubmit=1
            }
            if(value.length > 11){
                this.phone=value.slice(0,11)
            }
        },
    },
    mounted(){
        setTimeout(() => {
            this.ShowLater=1
        }, 250);
        this.$addr.get(`user/get_by_uid`).then(
            response=>{
                console.log('获取用户数据请求成功',response.data);
                if(response.data.data.phone != null){
                    this.oldphone=response.data.data.phone
                }
                if(response.data.data.username != null){
                    this.username=response.data.data.username
                }
            },error=>{
                if(!this.logoutShow){
                    this.$message({
                        message:'获取用户数据请求失败，请重新登陆',
                        type:'error'
                    })
                    console.log("获取用户数据请求失败",error.message);
                    //this.$router.replace({name:'login'})
                }
            }
        )
    },
}
</script>

<style scoped>
.div1{
    background: linear-gradient(to right,rgb(255, 182, 238),rgb(255, 113, 236));
    color: white;
    text-align: center;
    width: 100%;
    padding-top: 23px;
    padding-bottom: 15px;
    user-select: none;
}
.img_back{
    position: fixed;
    margin-top: 28px;
    left: 14px;
    height: 18px;
}
.zhezhao{
    width: 100%;
    height: 100%;
    background-color: rgb(252, 245, 252);
    opacity: 0.4;
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
}
.div2{
    padding-top: 20px;
    background-color: rgb(255, 255, 255);
    font-size: 17px;
    padding-bottom: 25px;
    position: fixed;
    margin-left: -10px;
    padding-left: 25px;
    width: 100%;
    box-shadow: 0px 2px 4px 0.1px rgb(236, 236, 236);
}
.input{
    margin-left: 15px;
    outline: none;
    border: 0px;
    font-size: 16px;
    width: 70%;
    background-color: rgb(255, 255, 255);
    padding-top: 15px;
    padding-bottom: 15px;
}
.img_false{
    position: absolute;
    height: 15px;
    margin-top: 17px;
}
.div3{
    position: absolute;
    width: 94%;
    font-size: 18px;
    text-align: center;
    padding-bottom: 15px;
    padding-top: 15px;
    margin-left: 10px;
    border-radius: 5px;
    background-color: white;
    box-shadow: 0px 1px 5px 1px rgb(212, 212, 212) ;
    /* margin-top: -310px; */
    margin-top: -39px;
}
.img_error{
    height: 26px;
    position: absolute;
    margin-left: -15px;
    margin-top: 1px;
}
.div4{
    margin-top: 200px;
    background-color: rgb(255, 206, 236);
    padding-top: 12px;
    padding-bottom: 12px;
    margin-left: 12px;
    margin-right: 12px;
    border-radius: 8px;
    color: white;
    text-align: center;
}
.img_phone{
    position:fixed;
    top: 40px;
    right: -150px;
    z-index: -1;
    transition: opacity .8s ease-out;
}
</style>